####################################################################################################################################################
#Purpose: To get annotations for markers using annovar
#Date: 11-14-2012
#####################################################################################################################################################

use Getopt::Long;

&Getopt::Long::GetOptions(
'BUILD=s' => \$build,
'INPUT=s' => \$input,
'TEMP=s' => \$temp,
'ANNOT=s' => \$annot,
'DIR_ANNOVAR=s' => \$annovar_bin_dir,
'OUTPUT_DIR=s' => \$output_dir
);
if($build eq "" || $input eq "" || $temp eq "" || $annot  eq "" || $annovar_bin_dir eq "" || $output_dir eq "")
{
        print "missing arguments\n USAGE : perl perl_getannotation_annovar.pl  -BUILD <BUILD VERSION : 18/19> -INPUT <ANNOVAR INPUT FILE> -TEMP <PATH TO TEMP DIRECTORY> -ANNOT <ANNOT TYPE> -DIR_ANNOVAR <PATH TO ANNOVAR BIN DIRECTORY> -OUTPUT_DIR <PATH TO THE OUTPUT DIR>\nAnnotation Types:\n";
print "GENE_ANNOT_REFGENE-> 'geneanno__gene'\n";
print "GENE_ANNOT_UCSC-> 'geneanno__knowngene'\n";
print "GENE_ANNOT_ENSEMBL-> 'geneanno__ensgene'\n";
print "REGION_BASED_ANNOT_MOST_CONSERVED_ELEMENT-> 'regionanno__mce44way'\n";
print "REGION_BASED_ANNOT_TRANSCRIPT_FACTOR_BINDING_SITE-> 'regionanno__tfbs'\n";
print "REGION_BASED_ANNOT_CYTO_BAND-> 'regionanno__band'\n";
print "REGION_BASED_ANNOT_SEG_DUP-> 'regionanno__segdup'\n";
print "REGION_BASED_ANNOT_STRUCT_VAR_DGV-> 'regionanno__dgv'\n";
print "REGION_BASED_ANNOT_PUBLISHED_GWAS-> 'regionanno__gwascatalog'\n";
print "FILTER_BASED_ANNOT_1000genome-> 'filter__1000g2012apr'\n";
print "FILTER_BASED_ANNOT_dbSNP annotations-> 'filter__snp130__webfrom'\n";
print "FILTER_BASED_ANNOT_SIFT_POLYPHEN annotations-> 'filter__avsift'\n"; 
print "FILTER_BASED_ANNOT_PolyPhen2, MutationTaster, LRT, PhyloP annotations-> 'filter__ljb_pp2'\n"; 
print "FILTER_BASED_ANNOT_exome sequencing project annotations-> 'filter__esp6500_all__webfrom'\n";
print "FILTER_BASED_ANNOT_GERP++ annotations-> 'filter__gerp++gt2__webfrom'\n";
#print "FILTER_BASED_ANNOT_CG (complete genomics) frequency annotations-> 'filter__cg46__webfrom'\n";
#print "FILTER_BASED_ANNOT_Generic mutation annotations -> 'filter__generic -genericdbfile'\n";
die "Retry with new paramaters\n";
}
chomp($build);
chomp($input);
chomp($temp);
chomp($annot);
print "Input Parameters BUILD :HG$build INPUT:$input TEMP:$temp ANNOT:$annot\n";
unless(-d $temp)
{
        mkdir $temp or die "not able create $temp\n";
}
$pwde=`pwd`;
chomp($pwde);
#die "test $pwde\n";
print "downloading required annotation file to temp folder\n";
$pwd='$PWD';
chdir ($temp) or die "Error: the directory $temp cannot be accessed\n";
@annot=split("__",$annot);
if($annot[2] eq "webfrom")
{
	$annot[2] ="-webfrom annovar";
}
else
{
	$annot[2] ="";
}
$sys = "perl $annovar_bin_dir/annotate_variation.pl -downdb $annot[1] . $annot[2] -buildver hg$build\n";
print "$sys\n";
system($sys);
#die;
print "input $input\n";
if($input !~ m/\//)
{
	print "cp $pwde/$input  input.txt\n";
        system("cp $pwde/$input  input.txt");
}
else
{
        system("cp $input  input.txt");
}
system("perl $annovar_bin_dir/annotate_variation.pl -buildver hg$build -$annot[0] -dbtype $annot[1] input.txt $pwd");

=head
if($annot[0] eq "geneanno")
{
	system("perl $annovar_bin_dir/annotate_variation.pl -downdb -buildver hg$build $annot[1] .");
	system("cp $input  input.txt");
	system("perl $annovar_bin_dir/annotate_variation.pl -buildver hg$build -$annot[0] -dbtype $annot[1] input.txt $pwd");
	system("");
}
elsif($annot[0] eq "regionanno")
{
        system("perl $annovar_bin_dir/annotate_variation.pl -downdb -buildver hg$build $annot[1] .");
        print "input $input\n";
	if($input !~ m/\//)
	{
		print "cp $pwde/$input  input.txt\n";
		system("cp $pwde/$input  input.txt");
        }
	else
	{
		
		system("cp $input  input.txt");
	}
	system("perl $annovar_bin_dir/annotate_variation.pl -buildver hg$build -$annot[0] -dbtype $annot[1] input.txt $pwd");
        #system("");
}
=head
elsif($annot eq "geneanno_ensgene")
{
        system("perl $annovar_bin_dir/annotate_variation.pl -downdb -buildver hg$build ensGene .");
        print "input $input\n";
        if($input !~ m/\//)
        {
                print "cp $pwde/$input  input.txt\n";
                system("cp $pwde/$input  input.txt");
        }
        else
        {

                system("cp $input  input.txt");
        }
        system("perl $annovar_bin_dir/annotate_variation.pl -buildver hg$build -geneanno -dbtype ensGene input.txt $pwd");
        #system("");
}
elsif($annot eq "")
{
        system("perl $annovar_bin_dir/annotate_variation.pl -downdb -buildver hg$build ensGene .");
        print "input $input\n";
        if($input !~ m/\//)
        {
                print "cp $pwde/$input  input.txt\n";
                system("cp $pwde/$input  input.txt");
        }
        else
        {

                system("cp $input  input.txt");
        }
        system("perl $annovar_bin_dir/annotate_variation.pl -buildver hg$build -geneanno -dbtype ensGene input.txt $pwd");
        #system("");
}

